package com.ld.hpfs.view.excel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.ld.hpfs.common.utils.Tools;

public class SearchSumExcelView extends AbstractExcelView{
	@Override
    protected void buildExcelDocument(Map model, HSSFWorkbook workbook, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        // // 取得模型数据
        // SqlRowSet table = (SqlRowSet) model.get("table");
        // String title = model.get("title").toString();

        // 取得模型数据
    	ArrayList<HashMap<String,Object>> table = (ArrayList<HashMap<String,Object>>) model.get("table");
 //       String title = "代码分析表";

        // 创建工作表和标题
        HSSFSheet sheet = workbook.createSheet(); // 创建工作区
//        HSSFRow row_title = sheet.createRow(0); // 创建一行引用对象
//        HSSFFont title_font = workbook.createFont(); // 创建标题的字体
//
//        title_font.setFontHeightInPoints((short) 8);
//        title_font.setFontHeight((short) HSSFFont.BOLDWEIGHT_NORMAL);
//        title_font.setColor((short) (HSSFFont.COLOR_RED));
//
//        HSSFCellStyle title_style = workbook.createCellStyle(); // 创建样式
//        title_style.setFont(title_font);
//
//        HSSFCell cell_title = row_title.createCell(1); // 创建单元格引用对象
//        cell_title.setCellStyle(title_style);
//        cell_title.setCellValue(title);

        // 创建数据表头
        String titles[] = { "期间", "借方","贷方","当月金额","累计金额"};

        HSSFCellStyle cellStyle = workbook.createCellStyle(); 
       // HSSFDataFormat format = workbook.createDataFormat(); 
        cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("@")); 
        cellStyle.setAlignment((short) HSSFCellStyle.ALIGN_RIGHT);
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle items_style = workbook.createCellStyle();
        items_style.setAlignment((short) HSSFCellStyle.ALIGN_CENTER);

        HSSFFont celltbnamefont = workbook.createFont();
        celltbnamefont.setFontHeightInPoints((short) 10);

        celltbnamefont.setColor((short) (HSSFFont.COLOR_RED));
        items_style.setFont(celltbnamefont);
        items_style.setWrapText(true);

        for (int i = 0; i < titles.length; i++) {
            HSSFCell cell = row.createCell(i);
            sheet.setColumnWidth(i, 5335);
            cell.setCellValue(titles[i]);
            cell.setCellStyle(items_style);
        }

        HSSFCellStyle datestyle = workbook.createCellStyle();
        HSSFDataFormat df = workbook.createDataFormat();
        datestyle.setDataFormat(df.getFormat("yyyy-mm-dd"));

        int rowNum = 1;
        for (HashMap<String,Object> entry : table) {
            // create the row data
        	
            HSSFRow datarow = sheet.createRow(rowNum++);
            datarow.createCell(0).setCellStyle(cellStyle);
            datarow.createCell(1).setCellStyle(cellStyle);
            datarow.createCell(2).setCellStyle(cellStyle);
            datarow.createCell(3).setCellStyle(cellStyle);
            datarow.createCell(4).setCellStyle(cellStyle);
            if(entry.get("num")!=null){
            	datarow.createCell(0).setCellValue(entry.get("num").toString());
            }
            if(entry.get("db")!=null){
            	datarow.createCell(1).setCellValue(entry.get("db").toString());
            }
            if(entry.get("cr")!=null){
            	datarow.createCell(2).setCellValue(entry.get("cr").toString());
            }
            if(entry.get("amount")!=null){
            	datarow.createCell(3).setCellValue(entry.get("amount").toString());
            }
            if(entry.get("sum_amount")!=null){
            	datarow.createCell(4).setCellValue(entry.get("sum_amount").toString());
            }
        }
    }
}
